package com.ruoyi.web.controller.system;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Hr;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Job;
import com.ruoyi.system.service.IJobService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;



import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;


import com.ruoyi.system.domain.Seeker;
import com.ruoyi.system.service.ISeekerService;


import static java.lang.Float.isNaN;

/**
 * 职位信息管理Controller
 * 
 * @author ruoyi
 * @date 2024-05-22
 */
@RestController
@RequestMapping("/system/job")
public class JobController extends BaseController
{
    @Autowired
    private IJobService jobService;
    @Autowired
    private ISeekerService SeekerService;
    /**
     * 查询职位信息管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Job job)
    {
        startPage();
        List<Job> list = jobService.selectJobList(job);
        return getDataTable(list);
    }

    @GetMapping("/listBy")
    public TableDataInfo listBy(String jobName,String jobWorkaddr,
    String jobPoststat,String jobEdureq,String jobInfoupdatetime,
    String jobSalrange,String jobWorkexp,String jobDesc)
    {
        startPage();
        Job job = new Job();
        job.setJobName(jobName);
        job.setJobWorkaddr(jobWorkaddr);
        job.setJobPoststat(jobPoststat);
        job.setJobEdureq(jobEdureq);
        job.setJobInfoupdatetime(jobInfoupdatetime);
        job.setJobSalrange(jobSalrange);
        job.setJobWorkexp(jobWorkexp);
        job.setJobDesc(jobDesc);
        
        List<Job> list = jobService.selectJobList(job);
        return getDataTable(list);
    }

    @GetMapping("/recommend")
    public TableDataInfo recommend(@RequestParam("seekerId")String seekerId)
    {
        startPage();

        Long seekerIdLong;
        seekerIdLong = Long.parseLong(seekerId);

        Seeker seeker =SeekerService.selectSeekerBySeekerId(seekerIdLong);

        
        List<Job> list = jobService.selectRecommendedJobs(seeker);
        return getDataTable(list);
    }


    @GetMapping("/researchCommend")
public TableDataInfo researchCommend(String jobName, String jobWorkaddr,
                            String jobPoststat, String jobEdureq,
                            String jobInfoupdatetime, String jobSalrange,
                            String jobWorkexp, String jobDesc) {
    startPage();
    Job searchCriteria = new Job();
    searchCriteria.setJobName(jobName);
    searchCriteria.setJobWorkaddr(jobWorkaddr);
    searchCriteria.setJobPoststat(jobPoststat);
    searchCriteria.setJobEdureq(jobEdureq);
    searchCriteria.setJobInfoupdatetime(jobInfoupdatetime);
    searchCriteria.setJobSalrange(jobSalrange);
    searchCriteria.setJobWorkexp(jobWorkexp);
    searchCriteria.setJobDesc(jobDesc);

    List<Job> list = jobService.SearchRecommendedJobs(searchCriteria);
    return getDataTable(list);
}


    @GetMapping(value = "/jobInfo/{jobId}")
    public AjaxResult jobInfo(@PathVariable("jobId") Long jobId)
    {
        return success(jobService.selectJobByJobId(jobId));
    }



    @GetMapping(value = "/jobdetail/{jobId}")
    public AjaxResult jobdetail(@PathVariable("jobId") Long jobId)
    {

        //Long jobIdLong = Long.parseLong(jobId);

        Job job=jobService.selectJobByJobId(jobId);
        Map<String,Object> data = new HashMap<>();
        data.put("job",job);
        System.out.println(jobId);
        System.out.println(job);
        return new AjaxResult(200,"返回成功",data);
    }

    @RequestMapping("/updatejobdetail")
    public AjaxResult updatejobdetail(
            @RequestParam String jobid,
            @RequestParam String title,
            @RequestParam String hrid,

            @RequestParam String poststat,
            @RequestParam String recruitstat,
            @RequestParam String jobkind,
            @RequestParam String updatetime,
            @RequestParam String salary,
            @RequestParam String loc,
            @RequestParam String educ,
            @RequestParam String jobdesc,
            @RequestParam String exprdesc){
        Long jobIdLong = Long.parseLong(jobid);
        Job jobPost = jobService.selectJobByJobId(jobIdLong);
        jobPost.setJobName(title);



        // 将 hrid 从 String 转换为 Long
        try {
            Long hrIdLong = Long.parseLong(hrid);
            jobPost.setHrId(hrIdLong);
        } catch (NumberFormatException e) {
            return AjaxResult.error("Invalid HR ID format.");
        }
        //jobPost.setJobId(jobid);
        //jobPost.setHrId(hrid);
        jobPost.setJobPoststat(poststat);
        jobPost.setJobRecruitstat(recruitstat);

        jobPost.setJobKind(jobkind);
        try {
            jobPost.setJobInfoupdatetime(updatetime);
        } catch (NumberFormatException e) {
            return AjaxResult.error("Invalid updatetime format.");
        }

        jobPost.setJobSalrange(salary);
        jobPost.setJobWorkaddr(loc);
        jobPost.setJobEdureq(educ);
        jobPost.setJobDesc(jobdesc);
        jobPost.setJobWorkexp(exprdesc);

        return toAjax(jobService.updateJob(jobPost));
    }



    @GetMapping("/listByhrid")
    public TableDataInfo listByhrid(Long hrId)
    {
        startPage();
        Job job = new Job();
        job.setHrId(hrId);
        List<Job> list = jobService.selectJobList(job);
        return getDataTable(list);
    }
    
    /**
     * 导出职位信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:job:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Job job)
    {
        List<Job> list = jobService.selectJobList(job);
        ExcelUtil<Job> util = new ExcelUtil<Job>(Job.class);
        util.exportExcel(response, list, "职位信息管理数据");
    }

    /**
     * 获取职位信息管理详细信息
     */
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId)
    {
        return success(jobService.selectJobByJobId(jobId));
    }

    /**
     * 新增职位信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:job:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Job job)
    {
        return toAjax(jobService.insertJob(job));
    }

    /**
     * 修改职位信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:job:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Job job)
    {
        return toAjax(jobService.updateJob(job));
    }

    /**
     * 删除职位信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:job:remove')")
	@DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable Long[] jobIds)
    {
        return toAjax(jobService.deleteJobByJobIds(jobIds));
    }
}
